const crypto = require('crypto');
//aes128加密
export const aesEn = (data, key) => {
  let t1 = crypto.createCipheriv('aes-128-cbc', key, key);
  let crypted = t1.update(data, 'utf-8', 'hex');
  crypted += t1.final('hex');
  return crypted;
};

//aes128解密
export const aesDe = (crypted, key) => {
  let t2 = crypto.createDecipheriv('aes-128-cbc', key, key);
  let decrypted = t2.update(crypted, 'hex', 'utf8');
  decrypted += t2.final('utf8');
  return decrypted;
};

/**
 * @method 判断传入参数的类型
 * @param { any } param 传入需要判断的变量
 * @param { string } type 变量的类型
 * @returns { boolean }
 * @use judgeParamType("a", "string") / judgeParamType({}, "object")
 */
const judgeParamType = (param, type) => {
  return (
    Object.prototype.toString.call(param) ===
    `[object ${type.slice(0, 1).toUpperCase() + type.slice(1)}]`
  );
};

// console.log(unaes256encrypt(aes256encrypt("123456",cacheAK),cacheAK));
export default { aesDe, aesEn, judgeParamType };
